package net.lcqf.udp.send.message;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import net.lcqf.udp.game.RoomManager;
import net.lcqf.udp.kcp.Ukcp;
import net.lcqf.udp.request.SendOpKeyRequest;
import net.lcqf.udp.send.MessageBaseService;
import net.lcqf.udp.send.MessageFactory;
import net.lcqf.udp.send.RequestType;
import net.lcqf.udp.server.MsgInfo;
import net.lcqf.udp.utils.LocalCacheUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2021-2025 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2025/4/14 10:52
 * // +----------------------------------------------------------------------
 */

@Service
public class SendOpKey implements MessageBaseService {

    private static final Logger logger = LoggerFactory.getLogger(SendOpKey.class);

    @Override
    @PostConstruct
    public void init() {
        MessageFactory.register(RequestType.SEND_OP_KEY.getCustomOrdinal(), this);
    }

    @Override
    public Object sendMessage(Ukcp kcp, MsgInfo msgInfo) {
        SendOpKeyRequest request = JSONUtil.toBean(msgInfo.getData(), SendOpKeyRequest.class);

        RoomManager tClass = LocalCacheUtil.JOIN_ROOM.get(request.getRoomId());
        if (ObjectUtil.isNull(tClass)) {
            logger.error("房间号{}不存在", request.getRoomId());
            return null;
        }
        tClass.addOpKey(request.getOpKey());
        //logger.info("接收玩家移动、玩家释放技能数据：{}", request.getOpKey());

        return null;
    }
}
